﻿angular.module('qask').controller('ChangeTimeZoneController', ['$scope', function ($scope) {

    $scope.init = function (timezones, currentTimeZone) {
        $scope.timezones = timezones;
        $scope.currentTimeZone = currentTimeZone;
        $scope.initializeTimezoneSelect2();
    }

    $scope.initializeTimezoneSelect2 = function () {

        var timezoneElement = $("#Timezone");

        var timezoneList = [];
        angular.forEach($scope.timezones, function (val) {
            timezoneList.push({ "id": val.Id, "text": val.Location + ' ' + val.GMT });
        });

        timezoneElement.select2({
            data: timezoneList,
            initSelection: function (element, callback) {
                var data = { id: $scope.currentTimeZone, Text: timezoneList[$scope.currentTimeZone - 1].text };
                $("#select2-chosen-1").text(data.Text);
            }
        });

        timezoneElement.on("change", function (e) {
            var selectedTimezoneId = e.added.id;
            $("#TimeZoneId").val(selectedTimezoneId);
        });
    }

}]);
